package com.lzb.yabbs.net;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.lzb.yabbs.dao.Card;
import com.lzb.yabbs.dao.Comment;
import com.lzb.yabbs.interfaces.GetCardsCallback;
import com.lzb.yabbs.interfaces.GetCommentCallback;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuzaibing on 2015/9/8.
 */
public class GetCommentFromNet {
    public static void getComments(String url, final GetCommentCallback getCommentCallback,int id,int page){
        List<BasicNameValuePair> pairs=new ArrayList<>();
        BasicNameValuePair pair=new BasicNameValuePair("postId",id+"");
        BasicNameValuePair pair1=new BasicNameValuePair("page",page+"");
        pairs.add(pair);
        pairs.add(pair1);
        OkHttpClientRequest.requestGet(url, pairs, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                getCommentCallback.getCommentFail(0);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String resultStr=response.body().string();
                if(resultStr==null||resultStr.equals("null")) {
                    getCommentCallback.getCommentsSuccess(new ArrayList<Comment>());
                    return;
                }
                Log.d(getClass().getSimpleName(), resultStr);
                List<Comment> comments;
                comments= JSONObject.parseArray(resultStr, Comment.class);
                getCommentCallback.getCommentsSuccess(comments);
            }
        });
    }
}
